package de.innosystec.unrar;

import de.innosystec.unrar.exception.RarException;
import de.innosystec.unrar.io.IReadOnlyAccess;
import de.innosystec.unrar.io.ReadOnlyAccessFile;
import de.innosystec.unrar.rarfile.AVHeader;
import de.innosystec.unrar.rarfile.BaseBlock;
import de.innosystec.unrar.rarfile.BlockHeader;
import de.innosystec.unrar.rarfile.CommentHeader;
import de.innosystec.unrar.rarfile.EAHeader;
import de.innosystec.unrar.rarfile.EndArcHeader;
import de.innosystec.unrar.rarfile.FileHeader;
import de.innosystec.unrar.rarfile.MacInfoHeader;
import de.innosystec.unrar.rarfile.MainHeader;
import de.innosystec.unrar.rarfile.MarkHeader;
import de.innosystec.unrar.rarfile.ProtectHeader;
import de.innosystec.unrar.rarfile.SignHeader;
import de.innosystec.unrar.rarfile.SubBlockHeader;
import de.innosystec.unrar.rarfile.UnixOwnersHeader;
import de.innosystec.unrar.rarfile.UnrarHeadertype;
import de.innosystec.unrar.unpack.ComprDataIO;
import de.innosystec.unrar.unpack.Unpack;
import java.io.Closeable;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/* loaded from: classes2.dex */
public class Archive implements Closeable {
    private static Logger logger = Logger.getLogger(Archive.class.getName());
    private long arcDataCRC;
    private int currentHeaderIndex;
    private final ComprDataIO dataIO;
    private boolean encrypted;
    private EndArcHeader endHeader;
    private File file;
    private final List<BaseBlock> headers;
    private MarkHeader markHead;
    private MainHeader newMhd;
    private IReadOnlyAccess rof;
    private int sfxSize;
    private long totalPackedRead;
    private long totalPackedSize;
    private Unpack unpack;
    private final UnrarCallback unrarCallback;

    public Archive(File file) throws RarException, IOException {
        this(file, null);
    }

    public Archive(File file, UnrarCallback unrarCallback) throws RarException, IOException {
        this.headers = new ArrayList();
        this.markHead = null;
        this.newMhd = null;
        this.endHeader = null;
        this.arcDataCRC = -1L;
        this.encrypted = false;
        this.sfxSize = 0;
        this.totalPackedSize = 0L;
        this.totalPackedRead = 0L;
        setFile(file);
        this.unrarCallback = unrarCallback;
        this.dataIO = new ComprDataIO(this);
    }

    public File getFile() {
        return this.file;
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    public void setFile(File file) throws IOException {
        this.file = file;
        this.totalPackedSize = 0L;
        this.totalPackedRead = 0L;
        close();
        this.rof = new ReadOnlyAccessFile(file);
        try {
            readHeaders();
        } catch (Exception e) {
            logger.log(Level.WARNING, "exception in archive constructor maybe file is encrypted or currupt", (Throwable) e);
        }
        for (BaseBlock baseBlock : this.headers) {
            if (baseBlock.getHeaderType() == UnrarHeadertype.FileHeader) {
                this.totalPackedSize += ((FileHeader) baseBlock).getFullPackSize();
            }
        }
        UnrarCallback unrarCallback = this.unrarCallback;
        if (unrarCallback != null) {
            unrarCallback.volumeProgressChanged(this.totalPackedRead, this.totalPackedSize);
        }
    }

    public void bytesReadRead(int i) {
        if (i > 0) {
            this.totalPackedRead += i;
            UnrarCallback unrarCallback = this.unrarCallback;
            if (unrarCallback != null) {
                unrarCallback.volumeProgressChanged(this.totalPackedRead, this.totalPackedSize);
            }
        }
    }

    public IReadOnlyAccess getRof() {
        return this.rof;
    }

    public List<FileHeader> getFileHeaders() {
        ArrayList arrayList = new ArrayList();
        for (BaseBlock baseBlock : this.headers) {
            if (baseBlock.getHeaderType().equals(UnrarHeadertype.FileHeader)) {
                arrayList.add((FileHeader) baseBlock);
            }
        }
        return arrayList;
    }

    public FileHeader nextFileHeader() {
        BaseBlock baseBlock;
        int size = this.headers.size();
        do {
            int i = this.currentHeaderIndex;
            if (i >= size) {
                return null;
            }
            List<BaseBlock> list = this.headers;
            this.currentHeaderIndex = i + 1;
            baseBlock = list.get(i);
        } while (baseBlock.getHeaderType() != UnrarHeadertype.FileHeader);
        return (FileHeader) baseBlock;
    }

    public UnrarCallback getUnrarCallback() {
        return this.unrarCallback;
    }

    public boolean isEncrypted() {
        MainHeader mainHeader = this.newMhd;
        if (mainHeader != null) {
            return mainHeader.isEncrypted();
        }
        throw new NullPointerException("mainheader is null");
    }

    private void readHeaders() throws IOException, RarException {
        ProtectHeader protectHeader;
        EndArcHeader endArcHeader;
        this.markHead = null;
        this.newMhd = null;
        this.endHeader = null;
        this.headers.clear();
        this.currentHeaderIndex = 0;
        long length = this.file.length();
        while (true) {
            byte[] bArr = new byte[7];
            long position = this.rof.getPosition();
            if (position < length && this.rof.readFully(bArr, 7) != 0) {
                BaseBlock baseBlock = new BaseBlock(bArr);
                baseBlock.setPositionInFile(position);
                switch (baseBlock.getHeaderType()) {
                    case MarkHeader:
                        this.markHead = new MarkHeader(baseBlock);
                        if (!this.markHead.isSignature()) {
                            throw new RarException(RarException.RarExceptionType.badRarArchive);
                        }
                        this.headers.add(this.markHead);
                        break;
                    case MainHeader:
                        int i = baseBlock.hasEncryptVersion() ? 7 : 6;
                        byte[] bArr2 = new byte[i];
                        this.rof.readFully(bArr2, i);
                        MainHeader mainHeader = new MainHeader(baseBlock, bArr2);
                        this.headers.add(mainHeader);
                        this.newMhd = mainHeader;
                        if (!this.newMhd.isEncrypted()) {
                            break;
                        } else {
                            throw new RarException(RarException.RarExceptionType.rarEncryptedException);
                        }
                    case SignHeader:
                        byte[] bArr3 = new byte[8];
                        this.rof.readFully(bArr3, 8);
                        this.headers.add(new SignHeader(baseBlock, bArr3));
                        break;
                    case AvHeader:
                        byte[] bArr4 = new byte[7];
                        this.rof.readFully(bArr4, 7);
                        this.headers.add(new AVHeader(baseBlock, bArr4));
                        break;
                    case CommHeader:
                        byte[] bArr5 = new byte[6];
                        this.rof.readFully(bArr5, 6);
                        CommentHeader commentHeader = new CommentHeader(baseBlock, bArr5);
                        this.headers.add(commentHeader);
                        this.rof.setPosition(commentHeader.getPositionInFile() + commentHeader.getHeaderSize());
                        break;
                    case EndArcHeader:
                        int i2 = baseBlock.hasArchiveDataCRC() ? 4 : 0;
                        if (baseBlock.hasVolumeNumber()) {
                            i2 += 2;
                        }
                        if (i2 > 0) {
                            byte[] bArr6 = new byte[i2];
                            this.rof.readFully(bArr6, i2);
                            endArcHeader = new EndArcHeader(baseBlock, bArr6);
                        } else {
                            endArcHeader = new EndArcHeader(baseBlock, null);
                        }
                        this.headers.add(endArcHeader);
                        this.endHeader = endArcHeader;
                        return;
                    default:
                        byte[] bArr7 = new byte[4];
                        this.rof.readFully(bArr7, 4);
                        BlockHeader blockHeader = new BlockHeader(baseBlock, bArr7);
                        int i3 = AnonymousClass1.$SwitchMap$de$innosystec$unrar$rarfile$UnrarHeadertype[blockHeader.getHeaderType().ordinal()];
                        if (i3 != 1 && i3 != 2) {
                            if (i3 == 3) {
                                int headerSize = (blockHeader.getHeaderSize() - 7) - 4;
                                byte[] bArr8 = new byte[headerSize];
                                this.rof.readFully(bArr8, headerSize);
                                this.rof.setPosition(new ProtectHeader(blockHeader, bArr8).getPositionInFile() + protectHeader.getHeaderSize() + protectHeader.getDataSize());
                                break;
                            } else if (i3 == 4) {
                                byte[] bArr9 = new byte[3];
                                this.rof.readFully(bArr9, 3);
                                SubBlockHeader subBlockHeader = new SubBlockHeader(blockHeader, bArr9);
                                subBlockHeader.print();
                                switch (subBlockHeader.getSubType()) {
                                    case MAC_HEAD:
                                        byte[] bArr10 = new byte[8];
                                        this.rof.readFully(bArr10, 8);
                                        MacInfoHeader macInfoHeader = new MacInfoHeader(subBlockHeader, bArr10);
                                        macInfoHeader.print();
                                        this.headers.add(macInfoHeader);
                                        continue;
                                    case EA_HEAD:
                                        byte[] bArr11 = new byte[10];
                                        this.rof.readFully(bArr11, 10);
                                        EAHeader eAHeader = new EAHeader(subBlockHeader, bArr11);
                                        eAHeader.print();
                                        this.headers.add(eAHeader);
                                        continue;
                                    case UO_HEAD:
                                        int headerSize2 = ((subBlockHeader.getHeaderSize() - 7) - 4) - 3;
                                        byte[] bArr12 = new byte[headerSize2];
                                        this.rof.readFully(bArr12, headerSize2);
                                        UnixOwnersHeader unixOwnersHeader = new UnixOwnersHeader(subBlockHeader, bArr12);
                                        unixOwnersHeader.print();
                                        this.headers.add(unixOwnersHeader);
                                        continue;
                                }
                            } else {
                                logger.warning("Unknown Header");
                                throw new RarException(RarException.RarExceptionType.notRarArchive);
                            }
                        } else {
                            int headerSize3 = (blockHeader.getHeaderSize() - 7) - 4;
                            byte[] bArr13 = new byte[headerSize3];
                            this.rof.readFully(bArr13, headerSize3);
                            FileHeader fileHeader = new FileHeader(blockHeader, bArr13);
                            this.headers.add(fileHeader);
                            this.rof.setPosition(fileHeader.getPositionInFile() + fileHeader.getHeaderSize() + fileHeader.getFullPackSize());
                            break;
                        }
                        break;
                }
            } else {
                return;
            }
        }
    }

    public void extractFile(FileHeader fileHeader, OutputStream outputStream) throws RarException {
        if (!this.headers.contains(fileHeader)) {
            throw new RarException(RarException.RarExceptionType.headerNotInArchive);
        }
        try {
            doExtractFile(fileHeader, outputStream);
        } catch (Exception e) {
            if (e instanceof RarException) {
                throw ((RarException) e);
            }
            throw new RarException(e);
        }
    }

    private void doExtractFile(FileHeader fileHeader, OutputStream outputStream) throws RarException, IOException {
        this.dataIO.init(outputStream);
        this.dataIO.init(fileHeader);
        this.dataIO.setUnpFileCRC(isOldFormat() ? 0L : -1L);
        if (this.unpack == null) {
            this.unpack = new Unpack(this.dataIO);
        }
        if (!fileHeader.isSolid()) {
            this.unpack.init(null);
        }
        this.unpack.setDestSize(fileHeader.getFullUnpackSize());
        try {
            this.unpack.doUnpack(fileHeader.getUnpVersion(), fileHeader.isSolid());
            FileHeader subHeader = this.dataIO.getSubHeader();
            if (((-1) ^ (subHeader.isSplitAfter() ? this.dataIO.getPackedCRC() : this.dataIO.getUnpFileCRC())) == subHeader.getFileCRC()) {
                return;
            }
            throw new RarException(RarException.RarExceptionType.crcError);
        } catch (Exception e) {
            this.unpack.cleanUp();
            if (e instanceof RarException) {
                throw ((RarException) e);
            }
            throw new RarException(e);
        }
    }

    public MainHeader getMainHeader() {
        return this.newMhd;
    }

    public boolean isOldFormat() {
        return this.markHead.isOldFormat();
    }

    @Override // java.io.Closeable, java.lang.AutoCloseable
    public void close() throws IOException {
        IReadOnlyAccess iReadOnlyAccess = this.rof;
        if (iReadOnlyAccess != null) {
            iReadOnlyAccess.close();
            this.rof = null;
        }
        Unpack unpack = this.unpack;
        if (unpack != null) {
            unpack.cleanUp();
        }
    }
}
